package com.wechet.controller;

import com.lly835.bestpay.model.PayResponse;
import com.wechet.common.OrderStatus;
import com.wechet.dto.OrderDto;
import com.wechet.exception.SellException;
import com.wechet.serveice.OrderService;
import com.wechet.serveice.impl.PayServiceImpl;
import com.wechet.serveice.impl.orderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Created by sc on 2017/9/24.
 */
@Controller
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private orderServiceImpl orderService;

    @Autowired
    private PayServiceImpl payService;


    @GetMapping("/create")
    public ModelAndView create(@RequestParam("orderId") String order,
                               @RequestParam("returnUrl") String url,
                               Map<String, Object> map) {
        OrderDto orderDto = orderService.findOne(order);
        if (orderDto == null) {
            throw new SellException(OrderStatus.ORDER_NOT_EXIST);
        }
        //  PayResponse payResponse = payService.create(orderDto);
        map.put("returnUrl", url);
        //map.put("payResponse",payResponse);
        return new ModelAndView("pay/create", map);
    }


    @PostMapping("/notify")
    public ModelAndView notify(@RequestBody String notifydata) {
        payService.notify(notifydata);
        return new ModelAndView("pay/success");
    }


}
